function preEvalDataFactory () {
    this.tableData = null;
    this.activeBtn = 1;
    var me = this;
    // 获取蓄水动态评价表格数据
    this.loadData = function () {
        var def = $.Deferred();
        $.getJSON("/jsite-szy-dispatch/a/szy/dispatch/ddsWeP/listAllToMap", function (data) {
            me.tableData = data;
            def.resolve();
        });
        return def.promise();
    }
}

preEvalDataFactory.prototype = {
    // 从tableData中获取年份数据
    getYearsFromTableData: function () {
        var data = this.tableData;
        var result = [];
        for (var year in data) {
            result.push({
                'text': year,
                'value': year
            });
        }
        return result;
    },
    getChartData: function () {
        var data = this.tableData;
        var result = {},titleYear=1991,xAxisCate0=[],xAxisCate1=[],data0_1=[],data0_2=[],data0_3=[],data1_1=[],data1_2=[],data1_3=[];
        for (var year in data) {
            titleYear=parseInt(year);
            for(var temp=0,l=data[year].length;temp<l;temp++){
                var tempdata=data[year][temp];
                if(tempdata.regType==0){
                    xAxisCate0.push(tempdata.regNm);
                    data0_1.push(parseFloat(tempdata.lyP));
                    data0_2.push(parseFloat(tempdata.curP));
                    data0_3.push(parseFloat(tempdata.annP));
                }else if(tempdata.regType==1){
                    xAxisCate1.push(tempdata.regNm);
                    data1_1.push(parseFloat(tempdata.lyP));
                    data1_2.push(parseFloat(tempdata.curP));
                    data1_3.push(parseFloat(tempdata.annP));
                }
            }
         }
        result={
            "0":{
                title: titleYear+'年行政分区降水量图',
                xAxisCate:xAxisCate0,
                serieData: [{
                    name: (titleYear-1)+'年',
                    data: data0_1
                }, {
                    name: titleYear+'年',
                    data: data0_2
                }, {
                    name: '多年平均',
                    data: data0_3
                }]
            },
            "1":{
                title: titleYear+'年水资源分区降水量图',
                xAxisCate:xAxisCate1,
                serieData: [{
                    name: (titleYear-1)+'年',
                    data: data1_1
                }, {
                    name: titleYear+'年',
                    data: data1_2
                }, {
                    name: '多年平均',
                    data: data1_3
                }]
            }
        }
        return result;
    },
    setTableDataByType: function () {
        var year = Ext.getCmp("preEvalCombo").getValue();
        var currentData = this.tableData[year];
        var type = this.activeBtn;
        var grepData = $.grep(currentData, function (obj) {
            return obj.regType == type;
        });
        Ext.data.StoreManager.lookup('preEval_store1').setData(grepData);
    }
}
var preEvalData,preEvalChartData;

Ext.define('Admin.view.evaluation.preEval.PreEvalController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.preEvalController',
    views: ['Admin.view.evaluation.preEval.PreEval'],
    buildChart: function (dom) {
        var chartId = dom.el.id + "-body";
        _preEvalChart.init(chartId);
    },
    initData: function (_this, opt) {
        var myMask = new Ext.LoadMask(Ext.ComponentQuery.query('preEval')[0], { msg: "正在加载..." });
        myMask.show();
        var me = this;
        preEvalData = new preEvalDataFactory();
        $.when(preEvalData.loadData())
            .done(function () {
                _preEvalChart.dataMap=preEvalData.getChartData();
                // 设置年份数据
                var yearData = preEvalData.getYearsFromTableData();
                var firstSelect = yearData.length > 0 ? yearData[0].value : '';
                var year_store = Ext.create('Ext.data.Store', {
                    fields: ['text', 'value'],
                    data: yearData
                });
                Ext.create('Ext.form.ComboBox', {
                    renderTo: 'preEvalComboWrapper',
                    id: 'preEvalCombo',
                    labelWidth: 35,
                    emptyText: '请选择',
                    fieldLabel: "年份",
                    valueField: 'value',
                    displayField: 'text',
                    value: firstSelect,
                    editable: false,
                    store: year_store,
                    listeners: {
                        'change': me.changeYear
                    }
                });
                Ext.getCmp('preEvalBtn1').fireEvent('click');
                myMask.hide();
            });
    },
    changeYear: function (_this, newVal, oldVal, opt) {
        preEvalData.setTableDataByType();
    },
    switchAdminRegion: function () {
        const type = 0;
        preEvalData.activeBtn = type;
        preEvalData.setTableDataByType();
        _preEvalChart.chartType = "0";
        if (_preEvalChart.chartid !== null) {
            _preEvalChart.change();
        }
    },
    switchWaterRegion: function () {
        const type = 1;
        preEvalData.activeBtn = type;
        preEvalData.setTableDataByType();
        _preEvalChart.chartType = "1";
        if (_preEvalChart.chartid !== null) {
            _preEvalChart.change();
        }
    },

})












function preEvalChart(id) {
    this.dataMap = {
        '0': {
            title: '2017年行政分区降水量图',
            xAxisCate: ['临川区', '南城县', '黎川县', '南丰县', '崇仁县', '乐安县', '宜黄县'],
            serieData: [{
                name: '2016年',
                data: [118.203, 123.489, 129.034, 104.970, 131.744, 630.230, 530.230]
            }, {
                name: '2017年',
                data: [119.325, 123.438, 131.000, 121.594, 134.141, 681.807, 581.807]
            }, {
                name: '多年平均',
                data: [118.700, 123.465, 130.017, 113.282, 132.9425, 656.0185, 556.0185]
            }]
        },
        '1': {
            title: '2017年水资源分区降水量图',
            xAxisCate: ['盱江', '临水', '抚河中游廖坊以上', '抚河中游廖坊以下'],
            serieData: [{
                name: '2016年',
                data: [118.203, 123.489, 129.034, 104.970]
            }, {
                name: '2017年',
                data: [119.325, 123.438, 131.000, 121.594]
            }, {
                name: '多年平均',
                data: [118.700, 123.465, 130.017, 113.282]
            }]
        }
    };
    this.chartid = null;
    this.chartType = "0";
}

preEvalChart.prototype = {
    init: function (id) {
        var data = this.dataMap[this.chartType];
        this.chartid = id;
        $('#' + id).highcharts(this.getOption(data));
    },
    change: function () {
        var data = this.dataMap[this.chartType];
        $('#' + this.chartid).highcharts(this.getOption(data));
    },
    getOption: function (data) {
        return {
            chart: {
                type: 'column'
            },
            title: {
                text: data.title
            },
            xAxis: {
                categories: data.xAxisCate,
                crosshair: true
            },
            yAxis: {
                min: 0,
                title: {
                    text: '降水量 (mm)'
                }
            },
            tooltip: {
                headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
                footerFormat: '</table>',
                shared: true,
                useHTML: true
            },
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
                }
            },
            series: data.serieData,
            credits: { enabled: false }
        };
    }
}

var _preEvalChart = new preEvalChart();

